/*
 * @lc app=leetcode.cn id=80 lang=javascript
 *
 * [80] 删除有序数组中的重复项 II
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var removeDuplicates = function (nums) {
  let start = 0;
  let end = -1;
  let i = 0;

  while (i < nums.length) {
    if (nums[i] !== nums[end] || (nums[i] === nums[end] && nums[i] !== nums[end - 1])) {
      nums[end + 1] = nums[i];
      end++;
    }

    i++;
  }
  return end - start + 1;
};

// @lc code=end
